from lxml import etree
from lxml.etree import _Element

import requests


# 在运行环境为 Python 3.7.3中，需要安装的是 requests 2.27.0
# 大于这个版本会报错：urllib3 v2.0 only supports OpenSSL 1.1.1+, currently

def get_page(url: str):
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
    }

    resp = requests.get(url=url, headers=headers)
    html = resp.text
    return html


def parse_html(content: str):
    tree = etree.HTML(content)
    return tree


def parse_title(tree: _Element):
    return tree.xpath("/html/head/title/text()")[0]


def parse_table_head(tree: _Element):
    return tree.xpath('//*[@id="top20"]/thead/tr/th/text()')


def parse_second_table_head(tree: _Element):
    return tree.xpath('//*[@id="otherPL"]/thead/tr/th/text()')


def parse_table_body(tree: _Element):
    rows = tree.xpath('//*[@id="top20"]/tbody/tr')
    # print(len(rows))
    columns = list()
    for row in rows[0:20]:
        # print(etree.tostring(row))
        items = row.xpath('./td/text()')

        # for item in items:
        #     print(item)
        change = row.xpath('./td[3]/img/@src')
        # print(change)
        t = None
        if len(change) == 0:
            t = ''
        else:
            temp = str(change[0])
            if temp.find("down.png") >= 0:
                t = 'd'
            else:
                t = "u"
        # print(t)
        items.insert(2, t)
        columns.append(items)
    return columns


def parse_second_table_body(tree: _Element):
    rows = tree.xpath('//*[@id="otherPL"]/tbody/tr')
    columns = list()
    for row in rows:
        items = row.xpath('./td/text()')
        columns.append(items)
    return columns


def write_table(name: str, head: list, body: list):
    file_name = name + ".csv"
    with open(file=file_name, mode="w", encoding="utf-8") as f:
        h = ",".join(head)
        f.write(h)
        f.write("\n")

        for row in body:
            f.write(",".join(row))
            f.write("\n")


if __name__ == '__main__':
    url = "https://www.tiobe.com/tiobe-index/"

    # 下载网页内容
    html = get_page(url=url)
    parse_html(html)
    tree = parse_html(html)
    title = parse_title(tree)

    # heads = parse_table_head(tree)
    #
    # body = parse_table_body(tree)
    #
    # write_table(name=title, head=heads, body=body)

    second_heads = parse_second_table_head(tree)
    print(second_heads)

    second_body = parse_second_table_body(tree)

    for row in second_body:
        print(row)
